package midlet;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
//import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.IllegalModeException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.drewChanged.imaging.jpeg.JpegMetadataReader;
import com.drewChanged.imaging.jpeg.JpegProcessingException;
import com.drewChanged.metadata.Directory;
import com.drewChanged.metadata.Metadata;
import com.drewChanged.metadata.MetadataException;
//import com.drewChanged.metadata.Tag;
import com.drewChanged.metadata.exif.ExifDirectory;
//import com.drewChanged.metadata.exif.ExifReader;


/**
 * @author Antonia
 *
 */
public class TestThumbnail extends MIDlet implements CommandListener {

	Form mainList;
	ByteArrayInputStream bis;
	// filepath is hardcoded for now, but this method can always take filename as a parameter
	String fullPath = "file:///root1/Phone.JPG";
	
	/**
	 * 
	 */
	public TestThumbnail() {
		mainList = new Form("TestExifJPEGThumbnail", null);
		mainList.setCommandListener(this);
	}

	public static void main(String[] args) throws JpegProcessingException {

	}

	/**
	 * @return
	 */
	private ByteArrayInputStream getFileIS() {
		System.out.println("in getFileIS");

		FileConnection fc = null;
		InputStream is;
		is = null;
		byte[] raw = null;

		try {
		// Open file and stream
			fc = (FileConnection) Connector.open(fullPath);
			
			is = fc.openInputStream();
		// Create a byte array to hold file contents
			raw = new byte[(int) fc.fileSize()];
			is.read(raw, 0, raw.length);
			bis = new ByteArrayInputStream(raw);
			
			System.out.println("length of data: "+raw.length);
			System.out.println("bis.available: "+bis.available());
			
			is.close();
			fc.close();
		} catch(IOException ioe) {
			 ioe.printStackTrace();
		} catch (SecurityException se) {
			se.printStackTrace(); 
		} catch (IllegalModeException ie) {
			ie.printStackTrace();
		}
		return bis;
	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		InputStream is = getFileIS();
		Metadata metadata;
		String tLength ="", tOffset ="";
		byte[] thumb = null;
		try {
			metadata = JpegMetadataReader.readMetadata(is);
			System.out.println("metadata extracted...");
	        Directory directory = metadata.getDirectory(ExifDirectory.class);
	        System.out.println("directory: "+directory.getName());
	        
//	        Vector tags = directory.getTagIterator();
//	        System.out.println("tag iterator obtained! tag count, "+ directory.getTagCount()+" size: "+tags.size());
//	        for (int y=0; y<tags.size(); y++) {
//	        	Tag tag = (Tag)tags.elementAt(y);
	        	// use Tag.toString()
//	        	System.out.println(tag);
//	        }
	        
	        if (((ExifDirectory) directory).containsThumbnail()) {
	        	System.out.println("exif contains thumbnail!");
	        	thumb = ((ExifDirectory) directory).getThumbnailData();
	        	System.out.println("exif thumbnail obtained!");
	        }
	        else
	        	System.out.println("exif thumbnail is not available!");
	        
	        System.out.println("tData " + (directory.containsTag(ExifDirectory.TAG_THUMBNAIL_DATA) ? "y" : "n"));
	        System.out.println("tLength " + (directory.containsTag(ExifDirectory.TAG_THUMBNAIL_LENGTH) ? "y" : "n"));
	        System.out.println("tOffset " + (directory.containsTag(ExifDirectory.TAG_THUMBNAIL_OFFSET) ? "y" : "n"));
	        
	        thumb = directory.getByteArray(ExifDirectory.TAG_THUMBNAIL_DATA);
	        tLength = directory.getString(ExifDirectory.TAG_THUMBNAIL_LENGTH);
	        tOffset = directory.getString(ExifDirectory.TAG_THUMBNAIL_OFFSET);
			mainList.append("File: "+fullPath);
			mainList.append("Thumb length: "+tLength);
			mainList.append("Thumb offset: "+tOffset);
			Image i = Image.createImage(thumb, 0, thumb.length);
			mainList.append(i);
		} catch (JpegProcessingException e) {
			e.printStackTrace();
			mainList.append("JpegProcessingException Error");
		} catch (MetadataException e) {
			e.printStackTrace();
			mainList.append("MetadataException Error");
		}
		
		mainList.addCommand(new Command("Exit",Command.EXIT, 1));
		Display d = Display.getDisplay(this);
		d.setCurrent(mainList);
	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {
		
	}

	/* (non-Javadoc)
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		
	}

	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction(Command arg0, Displayable arg1) {
		if (arg0.getLabel().equals("Exit")) {
			try {
				bis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				bis = null;
			}

	        try {
				destroyApp(true);
			} catch (MIDletStateChangeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        notifyDestroyed();
		}

	}
	
	
}
